package com.snopy.leetcode.index1_1000.index1_100;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question67
 * @description 二进制求和
 *
 * 给你两个二进制字符串，返回它们的和（用二进制表示）。
 *
 * 输入为 非空 字符串且只包含数字1和0。
 *
 * 示例1:
 *
 * 输入: a = "11", b = "1"
 * 输出: "100"
 * 示例2:
 *
 * 输入: a = "1010", b = "1011"
 * 输出: "10101"
 *
 * @email 77912204@qq.com
 * @date 2022/04/03 0:34
 */
public class Question67 {
    public static void main(String[] args) {
        //String a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101";
        //String b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011";
        String str = addBinary2(Integer.toBinaryString(7),Integer.toBinaryString(9));
        System.out.println(str);
    }
    /**
     * @Description 二进制求和
     * @param a:
     * @param b:
     * @return: java.lang.String
     * @Date 2022-04-03
     * @Author txl77912204@gmail.com
     **/
    public static String addBinary2(String a, String b) {
        StringBuilder str = new StringBuilder();
        int len = Math.max(a.length(),b.length()),carry = 0;
        for (int i=0;i<len;i++){
            carry+=i<a.length()?a.charAt(a.length()-1-i)-'0':0;
            carry+=i<b.length()?b.charAt(b.length()-1-i)-'0':0;
            str.append((char) (carry%2+'0'));
            carry/=2;
        }
        if (carry>0){
            str.append('1');
        }
        str.reverse();
        return str.toString();
    }
    /**
     * @Description 二进制求和 位运算 未做出
     * @param a:
     * @param b:
     * @return: java.lang.String
     * @Date 2022-04-03
     * @Author txl77912204@gmail.com
     **/
    /*public static String addBinary(String a, String b) {
        StringBuilder str = new StringBuilder();

        int len = a.length();
        if (a.length()>b.length()){
            len = b.length();
            String tmp = b;
            b = a;
            a = tmp;
        }

        for (int i=0;i<len;i++){
            int abit = a.charAt(a.length()-1-i)-'0';
            int bbit = b.charAt(b.length()-1-i)-'0';
            int answer= abit^bbit;
            int car =abit&bbit<<1;
            if (!((abit^bbit)==0 && (abit&bbit)==1)){
                b.replace(a.charAt(a.length()-1-i),(char)(abit+bbit+'0'));
                return b;
            }else {

            }
        }
        return str.toString();
    }*/
}
